<?php

	//get the variables
	$userId = $_GET["userId"];
	$fileId = $_GET["fileId"];
	$privacyId = getPrivacy("Public");
	
	$query = "SELECT * FROM CS275.files WHERE id='$fileId'";
	
	//establish connection to MySQL
	$link = mysql_connect('localhost', 'root', 'admin');
	if(!$link){
		$successful = false;
		die('Could not connect: ' . mysql_error());	
	}
	
	//perform query
	$result = mysql_query($query, $link);
	
	
	$stack = array();
	while($row = mysql_fetch_assoc($result)){
		//if the file is public or you are the owner of it
		if($row["privacy"] == $privacyId || $row["userId"] == $userId){
			$stack["name"] = $row["name"];
			$stack["file"] = $row["file"];
			$stack["userId"] = $row["userId"];
			$stack["course"] = $row["course"];
			$stack["professor"] = $row["professor"];
			$stack["privacy"] = $row["privacy"];
			$stack["id"] = $row["id"];
		}
	}
	
	//free results
	mysql_free_result($result);
	
	//close link
	mysql_close($link);
	
	//output json
	echo(json_encode($stack));
	
	function getPrivacy($privacy){
		$query = "SELECT * FROM CS275.privacy WHERE privacy='$privacy'";
		
		//establish connection to MySQL
		$link = mysql_connect('localhost', 'root', 'admin');
		if(!$link){
			$successful = false;
			die('Could not connect: ' . mysql_error());	
		}
		
		//perform query
		$result = mysql_query($query, $link);
		
		$id = 1;
		//if we get results
		while($row = mysql_fetch_assoc($result)){
			$id = $row["id"];
		}
		
		//free results
		mysql_free_result($result);
		
		//close link
		mysql_close($link);
		
		//output json
		return $id;
	}

?>